<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '添加' : '编辑'"
      :visible.sync="info.isShow"
      @close="close"
    >
      <div>{{ user }}</div>
      <el-form :model="user" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="user.title"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <upload-img
            @sendFile="user.img = $event"
            ref="uploadImg"
          ></upload-img>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" type="primary" @click="add"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {reqbanneradd ,reqbannerinfo,reqbanneredit} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins/index";
export default {
  mixins: [formClose],
  props: ["info"],
  data() {
    return {
      user: {
        title: "",
        img: null,
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {},
  methods: {
    ...mapActions({ getList: "banner/getList" }),
    //清空表单
    empty() {
      this.user = {
        title: "",
        img: null,
        status: 1,
      };
      this.$refs.uploadImg.setImgUrl("");
    },
    //获取一条用户信息
    async getOne(id) {
      let { data: res } = await reqbannerinfo({ id });
      // console.log(res);
      if (res.code == 200) {
        this.user = res.list;
        this.user.id = id;
        this.$refs.uploadImg.setImgUrl(this.$pre + this.user.img);
      }
    },
    //添加
    async add() {
      let { data: res } = await reqbanneradd(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.getList();
      }
    },
    //修改
    async update() {
      let { data: res } = await reqbanneredit(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.getList();
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed #ccc;
  position: relative;
  h3 {
    text-align: center;
    line-height: 100px;
  }
  input,
  img {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }
  input {
    opacity: 0;
    background-color: transparent;
  }
}
</style>